/**
 * 主页入口文件
 */
define(function (require, exports, module) {


    var UI = require("js/UI"), $doc = $(document), $win = $(window);
    var $checkboxSelect = $(".checkboxSelect"), $currGame = $(".currGame"), $search = $(".search")
    var $listFilter = $(".listFilter"), $condition = $listFilter.find(".condition");

    //<editor-folder desc='刷新列表'>
    var filterFun = function () {
        var services = $condition.eq(0).find(".curr").map(function () {
            return $(this).text();
        }).get().join(",")
        var _price = $condition.eq(1).find(".curr").text();
        var _platform = $platformInst.values();
        var data = {service: services, price: _price, platform: _platform}
        UI.ajax("./package.json", "post", data, function (flag, json) {
        })
    }
    $checkboxSelect.find(".flash").click(function () {
        filterFun();
    })
    $condition.find("a").unbind("click.aa").bind("click.aa", function () {
        var $this = $(this), $currGame = $this.parents(".currGame")
        if ($currGame && $currGame[0]) {

            if (!$this.hasClass("all")) {
                $this.addClass("curr");
                $this.siblings(".all").removeClass("curr");
            } else {
                $this.siblings().removeClass("curr");
                $this.addClass("curr");
            }
            var services = $condition.eq(0).find(".curr").map(function () {
                return $(this).text();
            }).get().join(",")
            UI.cookieFun.setCookie("_serviceShopping", services)


        } else {
            $this.siblings().removeClass("curr");
            $this.addClass("curr");
        }
        filterFun();
    })
    //</editor-folder>

    //交易平台 多选
    var $platformInst = UI.selectedFun({
        $selected: $checkboxSelect,
        "type": "checkbox",
        "change": function (val) {
            UI.cookieFun.setCookie("_platform_", $platformInst.values())
        }
    });


    //<editor-folder desc='读取cookie 更新运营商和交易平台样式'>
    var serviceShopping_selected = UI.cookieFun.getCookieByName("_serviceShopping");
    var _platform_selected = UI.cookieFun.getCookieByName("_platform_")
    $platformInst.selected(_platform_selected);
    if (serviceShopping_selected) {
        var array = serviceShopping_selected.split(",");
        var $condA = $condition.eq(0).find("a").removeClass("curr");
        for (var i = 0; i < array.length; i++) {
            $condA.each(function () {
                var $this = $(this);
                if ($this.text() == array[i]) {
                    $this.addClass("curr");
                }
            })
        }
    }
    //</editor-folder>

    //选择游戏封装
    function selectGame($search) {
        var $name = $search.find(".tit .name"),
            $selectWrap = $search.find(".selectWrap"),
            $gameResult = $search.find(".gameResult"),
            $shopResult = $search.find(".shopResult")

        var $iptName = $search.find("input[name=game]"),
            $iptService = $search.find("input[name=service]");
        var $iptSearch = $gameResult.find(".iptSearch"),
            $letter = $gameResult.find(".letterList a"),
            $result = $gameResult.find(".result");


        //<editor-folder desc='搜索'>
        var reg_letter = /[^(A-Za-z)]+/g, url = "./package.json";
        var $ipt = $iptSearch.find("input").unbind("keyup").bind("keyup", function () {
            var $this = $(this), _val = $this.val();
            _val = _val.replace(reg_letter, "")
            $this.val(_val);
        })
        $iptSearch.find("span").click(function () {
            var _key = $.trim($ipt.val())
            searchGame(_key)
        })
        //字母筛选
        $letter.unbind("click").bind("click", function () {
            var $this = $(this), _text = $this.text();
            _text = $this.hasClass("all") ? "all" : _text;
            $letter.removeClass("curr");
            $this.addClass("curr");
            searchGame(_text)
        })
        //</editor-folder>

        var selectBind = function ($game) {
                $game.each(function () {
                    var $this = $(this), id = $this.attr("id"), _text = $this.text();
                    $this.unbind("click").bind("click", function () {
                        //热门
                        if ($this.hasClass("gl")) {
                            _text = $this.find("a").eq(1).text();
                            $iptName.val(id);
                            $gameSpan.text(_text);
                            $gameService.trigger("click.service")
                            return
                        }
                        var $gameResult = $this.parents(".gameResult");
                        //游戏
                        if ($gameResult && $gameResult[0]) {
                            $iptName.val(id);
                            $gameSpan.text(_text);
                            $gameService.trigger("click.service");
                        } else {//服务商
                            $iptService.val(id);
                            $gameService.text(_text);
                            $selectWrap.hide().removeClass("service");
                        }
                        $game.removeClass("curr");
                        $this.addClass("curr");
                    })
                })
            },
            searchGame = function (key) {
                UI.ajax(url, "post", {key: key}, function (flag, json) {
                    if (flag == "success") {
                        var data = json.data;
                        var $temp = $("<div></div>")
                        for (var i = 0; i < data.length; i++) {
                            var _data = data[i], $g = null;
                            if (key == "all") {
                                $g = $("<div class='gl'></div>").attr("id", _data.id)
                                $("<a href='#'><img width='56' height='56' src='" + _data.logo + "'/></a>").appendTo($g);
                                $("<a href='#'></a>").text(_data.name).appendTo($g);
                                $result.addClass("hot")
                            } else {
                                $g = $("<a href='#'></a>").text(_data.name).attr("id", _data.id);
                                $result.removeClass("hot")
                            }
                            $temp.append($g)
                        }
                        $result.empty().append($temp.children());
                        selectBind($result.children())
                    }
                })
            }


        $doc.unbind("click.def").bind("click.def", function () {
            $selectWrap.hide();
        })
        $search.click(function (e) {
            e.stopPropagation();
        })
        var $close = $selectWrap.find(".G_close").unbind("click.hide").bind("click.hide", function () {
            $selectWrap.hide();
        })


        //from提交
        $search.find("[type='submit']").click(function () {
            var _game = $.trim($iptName.val()), _service = $.trim($iptService.val());
            if (!_game) {
                $gameSpan.trigger("click.game");
                return false
            }
            $close.trigger("click.hide")
        })
        //<editor-folder desc='显示搜索容器'>
        var $gameSpan = $name.filter(".game").unbind("click.game").bind("click.game", function () {
            $selectWrap.removeClass("service").show();
            selectBind($gameResult.find(".result").children())
        });
        var $gameService = $name.filter(".service").unbind("click.service").bind("click.service", function () {
            var _val = $.trim($iptName.val());
            _val = $gameSpan.text();
            if (!_val || _val == "请选择游戏") {
                $gameSpan.trigger("click.game");
                return
            }
            $selectWrap.addClass("service").show();
            selectBind($shopResult.find(".result").children())
        });
        //</editor-folder>
    }

    selectGame($search);


    //<editor-folder desc='回到顶部'>
    var $toTop = $(".toTop"), topHeight = $toTop.outerHeight();
    $doc.unbind("scroll.top").bind("scroll.top", function () {
        if ($doc.data("data-animate")) {
            return
        }
        var scrollTop = $doc.scrollTop();
        var bottom = scrollTop == 0 ? (0 - topHeight) : 20
        $toTop.stop(true, true).animate({
            bottom: bottom,
        }, 300)
    })
    $doc.trigger("scroll.top");
    $toTop.unbind("click.top").bind("click.top", function () {
        $doc.data("data-animate", true);
        var winH = $win.outerHeight();
        $toTop.stop(true, true).animate({
            bottom: winH,
            opacity: 0.6
        }, 500, function () {
            $toTop.hide();
        })
        var one = false;
        $("html,body").animate({
            scrollTop: 0
        }, 500, function () {
            if (one) {
                return
            }
            one = true;
            $toTop.css({
                bottom: 0 - topHeight,
                opacity: 1
            }).show()
            $doc.removeData("data-animate")
        })
    })
    //</editor-folder>
})